package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.YuShouOrderBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YuShouOrderDetailsActivity extends BaseActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yushou_order_details2);
        initData();
        initTitle();
        bindViews();
        upLoad();
    }

    private YuShouOrderBean.InfoEntity infos = new YuShouOrderBean.InfoEntity();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("order_id", order_id);
        showDialog();

        XUtil.Post(Url.BOOKING_BOOKINGORDERDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YuShouOrderBean bean = gson.fromJson(result, YuShouOrderBean.class);
                        infos = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                setData();
                super.onFinished();
            }


            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    // Content View Elements

    private TextView order_num;
    private TextView order_status;
    private TextView order_time;
    private RecyclerView recycler;
    private TextView all_num;
    private TextView all_price;
    private TextView first_price;
    private TextView sum;
    private TextView has_num;
    private TextView name;

    // End Of Content View Elements

    private void bindViews() {

        order_num = (TextView) findViewById(R.id.order_num);
        order_status = (TextView) findViewById(R.id.order_status);
        order_time = (TextView) findViewById(R.id.order_time);
        all_num = (TextView) findViewById(R.id.all_num);
        all_price = (TextView) findViewById(R.id.all_price);
        first_price = (TextView) findViewById(R.id.first_price);
        sum = (TextView) findViewById(R.id.sum);
        has_num = (TextView) findViewById(R.id.has_num);
        name = (TextView) findViewById(R.id.name);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    private void setData() {
        order_num.setText("订单号：" + infos.getBudget_no());
        order_time.setText("下单时间：" + infos.getOrder_time());
        all_num.setText("共" + infos.getPackage_nums() + "套商品\t合计：");
        all_price.setText(infos.getTotal_fee());
        first_price.setText(infos.getFirst_need_money());
        sum.setText(infos.getSum_nums() + "套");
        name.setText(infos.getUser_name());
        has_num.setText(infos.getHas_nums() + "套");
        if ("0".equals(infos.getPay_status())) {
            order_status.setText("未支付");
        } else {
            order_status.setText(infos.getPay_type());
        }
        final List<YuShouOrderBean.InfoEntity.DataEntity> dataList = infos.getData();
        recycler.setAdapter(new BaseRVAdapter(this, infos.getData()) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.project_content_item2;
            }

            @Override
            public void onBind(BaseViewHolder viewHolder, int position2) {
                viewHolder.setTextView(R.id.get_num, dataList.get(position2).getHas_nums() + "套");
                if ("0".equals(dataList.get(position2).getHas_nums())) {
                    viewHolder.getTextView(R.id.get_num).setVisibility(View.GONE);
                } else {
                    viewHolder.getTextView(R.id.get_num).setVisibility(View.VISIBLE);
                }
                x.image().bind(viewHolder.getImageView(R.id.iv_img), dataList.get(position2).getBudget_package_img());
                viewHolder.setTextView(R.id.tv_goods_name, dataList.get(position2).getPackage_name());
                viewHolder.setTextView(R.id.tv_product, dataList.get(position2).getCate_name());
                viewHolder.setTextView(R.id.tv_goods_info, dataList.get(position2).getSku_info());
                viewHolder.setTextView(R.id.tv_price, "¥ " + dataList.get(position2).getPackage_money());
                viewHolder.setTextView(R.id.tv_goods_number, "x" + dataList.get(position2).getNums());
            }

        });
    }

    private String order_id;

    private void initData() {
        order_id = getIntent().getStringExtra("order_id");
    }


    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("预售商品详情");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
